package com.talking.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.bumptech.glide.Glide;
import com.talking.App;
import com.talking.R;
import com.talking.bean.ChatBean;
import com.talking.databinding.ItemChartMeBinding;
import com.talking.databinding.ItemChartOtherBinding;
import com.talking.model.Http;
import com.talking.presenter.UserPresenter;
import com.talking.utils.Utils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChatAdapter extends ArrayAdapter<ChatBean> {
    private static final int layoutId = R.layout.item_chart_me;
    private final Activity context;
    public ChatAdapter(Activity context, List<ChatBean> objects){
        super(context,layoutId,objects);
        this.context=context;
    }
    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ChatBean chatBean = getItem(position);
        @SuppressLint("ViewHolder") ItemChartMeBinding itemChartMeBinding = ItemChartMeBinding.inflate(LayoutInflater.from(context));
        @SuppressLint("ViewHolder") ItemChartOtherBinding itemChartOtherBinding=ItemChartOtherBinding.inflate(LayoutInflater.from(context));
        boolean isMe= chatBean.account.equals(App.sp.getString("account", ""));
        if (isMe){
            itemChartMeBinding.itemMeContent.setText(chatBean.content);
        }else {
            itemChartOtherBinding.itemOtherContent.setText(chatBean.content);
        }
        UserPresenter.getUserByAccount(chatBean.account,null, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) {
               context.runOnUiThread(() -> {
                   try {
                       JSONObject jo = new JSONObject(Objects.requireNonNull(response.body()).string());
                       if (isMe){
                           Glide.with(context).load(Http.BASE_URL + jo.getString("iconUrl")).apply(Utils.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(itemChartMeBinding.itemChatMeIcon);
                       }else {
                           Glide.with(context).load(Http.BASE_URL + jo.getString("iconUrl")).apply(Utils.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(itemChartOtherBinding.itemChatOtherIcon);
                       }
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               });
            }
        });
        if (isMe){
            return itemChartMeBinding.getRoot();
        }else {
            return itemChartOtherBinding.getRoot();
        }
    }
}